package com.koron.css2.projectCenter.impl;

import com.koron.css2.clientCenter.bean.UserOpenBean;
import com.koron.css2.clientCenter.mapper.OpenUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.newProjectCenter.bean.ProjectReceiptBean;
import com.koron.css2.newProjectCenter.bean.query.ProjectReceiptQueryBean;
import com.koron.css2.newProjectCenter.mapper.ProjectReceiptMapper;
import com.koron.css2.projectCenter.bean.PjConsultBean;
import com.koron.css2.projectCenter.bean.PjInstallBean;
import com.koron.css2.projectCenter.mapper.PjConsultMapper;
import com.koron.css2.projectCenter.mapper.PjInstallMapper;
import com.koron.css2.projectCenter.mapper.PjUseMatrMapper;
import com.koron.css2.projectCenter.service.PjConsultService;
import com.koron.css2.projectCenter.vo.PjMeterVO;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.workflow.ProcessInterface;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 用水咨询审批后续操作
 * @author 许海宜
 * 2020年3月25日
 */
public class PjInstallProcess implements ProcessInterface {

	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
		//领料转用料
		if("Process-task2".equals(fromNodeCode) && "check".equals(toNodeCode)) {
			PjInstallMapper installMapper = factory.getMapper(PjInstallMapper.class);
			PjInstallBean pjInstallBean = installMapper.selectById(billId);

			PjUseMatrMapper mapper = factory.getMapper(PjUseMatrMapper.class);
			mapper.del(pjInstallBean.getConsultId(), 2);
			mapper.insertByGetuse(pjInstallBean.getConsultId());
		}
		//验收完毕生成退押金单
		if("Process-task12".equals(fromNodeCode) && "open".equals(toNodeCode)) {
			PjInstallMapper installMapper = factory.getMapper(PjInstallMapper.class);
			PjInstallBean pjInstallBean = installMapper.selectById(billId);
			
			PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
			PjConsultBean pjConsultBean = pjConsultMapper.selectById(pjInstallBean.getConsultId());
			//获取收款单合计押金，作为预缴合计
			ProjectReceiptMapper totalMapper = factory.getMapper(ProjectReceiptMapper.class);
			ProjectReceiptQueryBean receipt = new ProjectReceiptQueryBean();
			receipt.setBillId(pjInstallBean.getConsultId());
			List<ProjectReceiptBean> totalReceipt = totalMapper.listTotal(receipt);
			double depositMoney = 0;
			receipt.setChargeType("4");
			List<ProjectReceiptBean> deposit = totalMapper.list(receipt);
			if(totalReceipt!=null && totalReceipt.size()>0) {
				depositMoney=totalReceipt.get(0).getDepositMoney()==null?0.0:totalReceipt.get(0).getDepositMoney();
			}
			if(depositMoney != 0 && (deposit==null || deposit.size()==0)) {
				pjConsultBean.setDepositMoney(-depositMoney);
				addProjectReceiptDepositMoney(factory, userInfo, pjConsultBean, pjInstallBean,"4");
			}
			
		}
		//TODO 立户审批节点-临时数据转正式数据
		if("Process-task13".equals(fromNodeCode) && ("END".equals(toNodeCode) || "preSetmt".equals(toNodeCode) )) {
			PjInstallMapper installMapper = factory.getMapper(PjInstallMapper.class);
			PjInstallBean pjInstallBean = installMapper.selectById(billId);
			
			OpenUserMapper openMapper = factory.getMapper(OpenUserMapper.class);
			PjConsultMapper cmapper = factory.getMapper(PjConsultMapper.class);
			List<PjMeterVO> list = cmapper.meterList(pjInstallBean.getConsultId());
			//查询用水咨询

			PjConsultBean pjConsultBean= cmapper.selectById(pjInstallBean.getConsultId());
			openMapper.userOpenRecordDeleteByPjBillNo(pjInstallBean.getBillNo());
			
			//获取Mapper实例
			UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
			List<UserOpenBean> tempList = new ArrayList<UserOpenBean>();
			String id = "";
			if(list.size()==0){
				PjMeterVO pjMeterVO = new PjMeterVO();
				pjMeterVO.setId(Tools.getObjectId());
				list.add(pjMeterVO);
			}
			for (int i=0 ; i<list.size();i++) {
				PjMeterVO pjMeterVO = list.get(i);
				UserOpenBean userOpen = new UserOpenBean();
				userOpen.setCreateInfo(userInfo);
				userOpen.setUserAccountTempId(pjConsultBean.getUserAccountTempId());
				userOpen.setUserCtmTempId(pjConsultBean.getUserCtmTempId());
				userOpen.setUserInfoTempId(pjMeterVO.getId());
				userOpen.setPjInstallBillNo(pjInstallBean.getBillNo());
				if(i==0) {
					id=userOpen.getId();
					//保存第一个到单据上，用户列表展示
					userOpen.setBillNo(Tools.getBillNo(factory, "USER_OPEN"));
					userOpen.setBillDate(Tools.getCurrentDate());
					openMapper.userOpenAdd(userOpen);
				}
				userOpen.setUserOpenId(id);
				tempList.add(userOpen);
				//openMapper.pjUserOpenAdd(userOpen);
				pjMeterVO.setOpenDate(Tools.getCurrentDate());

				userInfoMapper.userInfoUpdate(pjMeterVO, 2);
			}
			if(tempList.size()==0){
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "审批失败，未获取到需要新增的用户资料", null);
			}
			//插入详情关联列表数据
			openMapper.userOpenTempAddList(tempList);
		}
		/**
		 * 如果是补款 那就是我们白天讨论那样 ‘编制结算’之后生成工程单 ‘工程结算’收款之后自动走过
		 * 如果是退款 不生成工程收款单 线下处理，‘工程结算’节点 还是需要人工点击 提交按钮 选择退款方式 表示退款完成 然后工程结束
		 */
		/**
		 * 工程收款发票问题 结论：周期长的工程 那可能需要未结算之前就开发票 （涉及到每年税点不一样的问题）
		 */
		/**
		 * 讨论的时候，小袁说 需考虑工程暂停和工程取消的问题
		 * 工程暂停 通过新的工作流实现
		 * 工程取消 如果在‘工程收款’之后取消 那就直接跳到‘编制预算’节点，钱算清楚之后，再工程作废；如果在‘工程收款’前取消，那就直接作废
		 */
		//TODO 编制结算审批节点-多退少补-生成工程收款单
		if("Process-task10".equals(fromNodeCode) && "setmt".equals(toNodeCode)) {
			PjInstallMapper installMapper = factory.getMapper(PjInstallMapper.class);
			PjInstallBean pjInstallBean = installMapper.selectById(billId);
			
			PjConsultMapper pjConsultMapper = factory.getMapper(PjConsultMapper.class);
			PjConsultBean pjConsultBean = pjConsultMapper.selectById(pjInstallBean.getConsultId());
			
			ProjectReceiptMapper mapper = factory.getMapper(ProjectReceiptMapper.class);
			ProjectReceiptQueryBean queryBean = new ProjectReceiptQueryBean();
			queryBean.setBillId(billId);
			queryBean.setChargeType("2");
			List<ProjectReceiptBean> result = mapper.list(queryBean);
			
			if(result!= null && result.size()>0) {
				if(result.get(0).getChargeTime()==null 
						|| result.get(0).getChargeTime().length()==0) {
					mapper.deleteById(billId);
					if(pjInstallBean.getBackFillMoney()!=0) {
						pjConsultBean.setDepositMoney(0.0);
						pjConsultBean.setBudgetMoney(pjInstallBean.getBackFillMoney());
						addProjectReceipt(factory, userInfo, pjConsultBean, pjInstallBean,"2");
						pjInstallBean.setAccruedMoney(pjInstallBean.getBackFillMoney()+pjInstallBean.getAccruedMoney());
						installMapper.saveAccruedMoney(pjInstallBean);
					}
				}
			}else {
				if(pjInstallBean.getBackFillMoney()!=0) {
					pjConsultBean.setDepositMoney(0.0);
					pjConsultBean.setBudgetMoney(pjInstallBean.getBackFillMoney());
					addProjectReceipt(factory, userInfo, pjConsultBean, pjInstallBean,"2");
					pjInstallBean.setAccruedMoney(pjInstallBean.getBackFillMoney()+pjInstallBean.getAccruedMoney());
					installMapper.saveAccruedMoney(pjInstallBean);
				}
			}
			
			
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", null);
	}
	/**
	 * 新增工程收款单
	 * @param factory
	 * @param pjConsultBean
	 * @param userInfo
	 */
	public void addProjectReceiptDepositMoney(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean, PjInstallBean pjInstallBean,String chargeType) {
		//组装工程收款单
		ProjectReceiptBean projectReceiptBean = new ProjectReceiptBean();
		projectReceiptBean.setId(Tools.getObjectId());
		projectReceiptBean.setBillId(pjInstallBean.getId());
		projectReceiptBean.setBillNo(pjInstallBean.getBillNo());
		projectReceiptBean.setBillDate(CommonUtils.getCurrentDate());
		projectReceiptBean.setBillType(PjConsultService.INSTALL_RECEIPT);
		projectReceiptBean.setReceivableMoney(0.0d);//应收
		projectReceiptBean.setDepositMoney(pjConsultBean.getDepositMoney());//押金
		projectReceiptBean.setTotalMoney(pjConsultBean.getDepositMoney());
		projectReceiptBean.setChargeFlag(0);
		projectReceiptBean.setComments("用水安装单据编号：" + pjInstallBean.getBillNo());
		projectReceiptBean.setTenantId(userInfo.getCurWaterCode());
		projectReceiptBean.setCreateAccount(userInfo.getUserInfo().getAcount());
		projectReceiptBean.setCreateName(userInfo.getUserInfo().getName());
		projectReceiptBean.setChargeType(chargeType);
		
		//新增工程收款单
		ProjectReceiptMapper projectReceiptMapper = factory.getMapper(ProjectReceiptMapper.class);
		projectReceiptMapper.add(projectReceiptBean);
	}
	public void addProjectReceipt(SessionFactory factory, UserInfoBean userInfo, PjConsultBean pjConsultBean, PjInstallBean pjInstallBean,String chargeType) {
		//组装工程收款单
		ProjectReceiptBean projectReceiptBean = new ProjectReceiptBean();
		projectReceiptBean.setId(Tools.getObjectId());
		projectReceiptBean.setBillId(pjInstallBean.getId());
		projectReceiptBean.setBillNo(pjInstallBean.getBillNo());
		projectReceiptBean.setBillDate(CommonUtils.getCurrentDate());
		projectReceiptBean.setBillType(PjConsultService.INSTALL_RECEIPT);
		projectReceiptBean.setReceivableMoney(pjInstallBean.getBackFillMoney());//应收
		projectReceiptBean.setDepositMoney(0.0);//押金
		projectReceiptBean.setTotalMoney(pjInstallBean.getBackFillMoney());
		projectReceiptBean.setChargeFlag(0);
		projectReceiptBean.setComments("用水安装单据编号：" + pjInstallBean.getBillNo());
		projectReceiptBean.setTenantId(userInfo.getCurWaterCode());
		projectReceiptBean.setCreateAccount(userInfo.getUserInfo().getAcount());
		projectReceiptBean.setCreateName(userInfo.getUserInfo().getName());
		projectReceiptBean.setChargeType(chargeType);
		
		//新增工程收款单
		ProjectReceiptMapper projectReceiptMapper = factory.getMapper(ProjectReceiptMapper.class);
		projectReceiptMapper.add(projectReceiptBean);
	}
	
	@Override
	public MessageBean<?> before(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, int operation) {
		return null;
	}
	
	@Override
	public MessageBean<?> after(SessionFactory factory, UserInfoBean userInfo, String billId, String fromNodeCode, String toNodeCode) {
		return exec(factory, userInfo, billId, fromNodeCode, toNodeCode);
	}
}
